package com.yb.bussiness.member;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yb.base.dao.BaseDao;
import com.yb.base.dao.IBaseDao;
import com.yb.domain.member.TsMember;
import com.yb.util.comm.DateTimeUtil;

public class MemberServiceImpl implements MemberService {
    
    private IBaseDao<TsMember, Long> dao;
    
    public MemberServiceImpl() {
        dao = new BaseDao<TsMember, Long>(TsMember.class);
    }
    
    public boolean deleteByIdString(String idString) {
        boolean isFlag = true;
        if (idString == null || "".equals(idString))
            return false;
        String sql = "delete from t_s_member where id in (" + idString + ")";
        try {
            dao.delete(sql);
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return isFlag;
    }
    
    public Map<String, Object> findAll(TsMember formBean, String page, String row) {
        String sql =
            "select tsm.id,tss.username,tss.type,realName,tel,sex,phone,address,tsm.sysuserId,tsm.createTime from t_s_member tsm left join t_s_sysuser tss on tsm.sysuserId = tss.id where 1=1 ";
        String countSql = "select count(tss.id) from t_s_member tsm left join t_s_sysuser tss on tsm.sysuserId = tss.id where 1=1  ";
        if (formBean != null) {
            if (formBean.getUsername() != null && !"".equals(formBean.getUsername())) {
                sql += " and tss.username like '%" + formBean.getUsername() + "%' ";
                countSql += " and tss.username like '%" + formBean.getUsername() + "%' ";
            }
            
            if (formBean.getType()!=null && 0 != formBean.getType()) {
                sql += " and tss.type = " + formBean.getType();
                countSql += " and tss.type = " + formBean.getType();
            }
            if (formBean.getStartTime() != null && !"".equals(formBean.getStartTime())) {
                sql += " and tsm.createTime >= '" + formBean.getStartTime() + " 00:00:00'";
                countSql += " and tsm.createTime >= '" + formBean.getStartTime() + " 00:00:00'";
            }
            if (formBean.getEndTime() != null && !"".equals(formBean.getEndTime())) {
                sql += " and tsm.createTime <= '" + formBean.getEndTime() + " 23:59:59'";
                countSql += " and tsm.createTime <= '" + formBean.getEndTime() + " 23:59:59'";
            }
        }
        if (page != null && row != null) {
            int snum = Integer.parseInt(page) - 1;
            sql += " order by tsm.id desc  limit  " + (snum * Integer.parseInt(row)) + "," + row + " ";
        }
        List<TsMember> list = dao.findList(sql);
        List<TsMember> alist = new ArrayList<TsMember>();
        for (TsMember tsMember : list) {
            TsMember bean = new TsMember();
            bean.setAddress(tsMember.getAddress());
            bean.setCreatTimeStr(DateTimeUtil.switchDateToString(tsMember.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
            bean.setId(tsMember.getId());
            bean.setPhone(tsMember.getPhone());
            bean.setRealname(tsMember.getRealname());
            bean.setSex(tsMember.getSex());
            bean.setSysuserId(tsMember.getSysuserId());
            bean.setTel(tsMember.getTel());
            bean.setType(tsMember.getType());
            bean.setUsername(tsMember.getUsername());
            alist.add(bean);
        }
        
        int total = 0;
        if (list.size() != 0) {
            total = Integer.parseInt((dao.findObjectArryList(countSql).get(0))[0] + "");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list", alist);
        map.put("total", total);
        return map;
    }
    
    
    public void saveOrUpdate(TsMember tsMember) {
        if (tsMember.getId() == null){
          tsMember.setCreateTime(new Date());
        }
        dao.saveOrUpdate(tsMember);
    }
    
    
    public void onAutding(Long id, Integer type) {
    	String sql = "select realName,phone from t_s_member where sysuserId = ?";
    	List list = dao.findObjectArryList(sql, new Object[] { id });
    	String name = "";
    	String pho = "";
    	if(null!=list&&list.size()>0){
    		Object[] obj = (Object[]) list.get(0);
    		name = obj[0].toString();
    		pho = obj[1].toString();
    	}
    	sql = "update t_s_sysuser set type = ? where id = ?";
    	dao.update(sql, new Object[] { type, id });
        if(3==type){
	        sql = "update t_p_buy_record set userId = ? where name=? and pho=?";
	        dao.update(sql, new Object[] { id , name , pho });
	        sql = "update t_g_brand_member set userId = ? where name=? and tel=?";
	        dao.update(sql, new Object[] { id , name , pho });
        }
    }
    
    public TsMember read(Long id) {
        String sql = "select * from t_s_member where sysuserId = " + id;
        List<TsMember> list = dao.findList(sql);
        if (list.isEmpty())
            return null;
        return list.get(0);
    }


    public Integer loadpremind() {
        String sql = "select count(tss.id) from t_s_member tsm left join t_s_sysuser tss on tsm.sysuserId = tss.id where tss.type = 4";
        int total = Integer
        .parseInt((dao.findObjectArryList(sql).get(0))[0]
                .toString());
        return total;
    }
    
    public boolean delete(String id) {
        boolean isFlag = true;
        String sql = "delete from t_s_member where id ="+id;
        String sql1 = "delete from T_s_sysuser where id = (select sysuserId from t_s_member where id = "+id+")";
        try {
            dao.delete(sql);
            dao.delete(sql1);
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return isFlag;
    }
}
